//
//  AnmideViewController.m
//  LOL_JewelBox
//
//  Created by dllo on 15/5/5.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "AnmideViewController.h"
#import "NewsViewController.h"
#import "VideoViewController.h"
#import "HeroViewController.h"
#import "ToolsViewController.h"
@interface AnmideViewController ()
@property (nonatomic, retain) UITabBarController *tabBarController;
@property (nonatomic, retain) UIImage *flakeImage;
@property (nonatomic, retain) UIImageView *flakeView;
@property (nonatomic, assign) NSInteger endFlag;

@end

@implementation AnmideViewController
@synthesize timer;
@synthesize splashImageView;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 设置视图大小
    UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    self.view = view;
    [view release];
    self.endFlag = 0;
    
    
    
    // 添加第一张图片
    splashImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dengluback"]];
    [UIView transitionWithView:splashImageView duration:5.f options:UIViewAnimationOptionTransitionCurlDown animations:^{
        
        splashImageView.frame = self.view.bounds;
    } completion:^(BOOL finished) {
        
    }];
    [self.view addSubview:splashImageView];
//    self.tabBarVC = [[UITabBarController alloc] init];
    
    // TabBar控制器
     self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.tabBar.barTintColor = [UIColor colorWithRed:42/255.0 green:42/255.0 blue:42/255.0 alpha:1];
    self.tabBarController.tabBar.translucent = NO;
    
    
    // 主页
    // 将主页透明度设为0
    self.tabBarController.view.alpha = 0.0;
    [self.view addSubview:self.tabBarController.view];
    [_tabBarController release];
    
    
    // 设置定时器结束动画
    timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(fadeScreen) userInfo:nil repeats:NO];
    
    //添加雪花的动画
    self.flakeImage = [UIImage imageNamed:@"iconfont-xue.png"];
    self.flakeView.backgroundColor = [UIColor clearColor];
    [NSTimer scheduledTimerWithTimeInterval:(0.2) target:self selector:@selector(play) userInfo:nil repeats:YES];

}

//动画
- (void)play {
    if (self.endFlag == 0) {
        
        //  NSLog(@"begin");
        self.flakeView = [[UIImageView alloc] initWithImage:self.flakeImage];
        int startX = round(random() % 1024);
        int endX = round(random() % 1024);
        double scale = 1 / round(random() % 100) + 1.0;
        double speed = 1 / round(random() % 100) + 1.0;
        self.flakeView.frame = CGRectMake(startX, -100.0, 25.0 * scale, 25.0 * scale);
        self.flakeView.alpha = 0.5;
        [self.view addSubview:self.flakeView];
        [UIView beginAnimations:nil context:self.flakeView];
        [UIView setAnimationDuration:10 * speed];
        self.flakeView.frame = CGRectMake(endX, 768.0, 25.0 * scale, 25.0 * scale);
        [UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context:)];
        [UIView setAnimationDelegate:self];
        [UIView commitAnimations];
        
    }
    
}
- (void)onAnimationComplete:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    
    self.flakeView = context;
    [self.flakeView removeFromSuperview];
    [_flakeView release];
}




- (void)fadeScreen
{
    [UIView beginAnimations:nil context:nil]; // begins animation block
    // 动画存在时间
    [UIView setAnimationDuration:1];        // sets animation duration
    [UIView setAnimationDelegate:self];        // sets delegate for this block
    [UIView setAnimationDidStopSelector:@selector(finishedFading)];   // calls the finishedFading method when the animation is done (or done fading out)
    self.view.alpha = 0.0;       // Fades the alpha channel of this view to "0.0" over the animationDuration of "0.75" seconds
    [UIView commitAnimations];   // commits the animation block.  This Block is done.
}

-(void)finishedFading
{
    
    [UIView beginAnimations:nil context:nil]; // begins animation block
    self.endFlag = 1;
    
    // 1.新闻视图控制器\导航栏\底部菜单
    NewsViewController *news = [[NewsViewController alloc] init];
    UINavigationController *newsNavi = [[UINavigationController alloc] initWithRootViewController:news];
    news.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"资讯" image:[UIImage imageNamed:@"News"] tag:1000];
    newsNavi.navigationBar.translucent = NO;
    newsNavi.navigationBar.barTintColor = [UIColor colorWithRed:60/255.0 green:60/255.0 blue:60/255.0 alpha:1];
        
    // 2.视频视图控制器\导航栏\底部菜单
    VideoViewController *video = [[VideoViewController alloc] init];
    UINavigationController *videoNavi = [[UINavigationController alloc] initWithRootViewController:video];
    videoNavi.navigationBar.translucent = NO;
    videoNavi.navigationBar.barTintColor = [UIColor colorWithRed:60/255.0 green:60/255.0 blue:60/255.0 alpha:1];
    video.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"视频" image:[UIImage imageNamed:@"Videos"] tag:1001];
    
    // 3.英雄视图控制器\导航栏\底部菜单
    HeroViewController *hero = [[HeroViewController alloc] init];
    UINavigationController *heroNavi = [[UINavigationController alloc] initWithRootViewController:hero];
    heroNavi.navigationBar.translucent = NO;
    heroNavi.navigationBar.barTintColor = [UIColor colorWithRed:60/255.0 green:60/255.0 blue:60/255.0 alpha:1];
    hero.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"英雄" image:[UIImage imageNamed:@"Heroes"] tag:1002];
    
    // 4.百科工具视图控制器\导航栏\底部菜单
    ToolsViewController *tools = [[ToolsViewController alloc] init];
    UINavigationController *toolsNavi = [[UINavigationController alloc] initWithRootViewController:tools];
    toolsNavi.navigationBar.translucent = NO;
    toolsNavi.navigationBar.barTintColor = [UIColor colorWithRed:60/255.0 green:60/255.0 blue:60/255.0 alpha:1];
    tools.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"百科工具" image:[UIImage imageNamed:@"Tools"] tag:1003];
    
    
    
    // 将导航栏加入tabbarController中
    
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:newsNavi,videoNavi,heroNavi,toolsNavi, nil];
    
    // 内存释放
    [news release];
    [newsNavi release];
    [video release];
    [videoNavi release];
    [hero release];
    [heroNavi release];
    [tools release];
    [toolsNavi release];
    
    [self.flakeView removeFromSuperview];
    
    
    // 显示主页时间
    [UIView setAnimationDuration:0.75];        // sets animation duration
    self.view.alpha = 1.0;   // fades the view to 1.0 alpha over 0.75 seconds
    self.tabBarController.view.alpha = 1.0;
    [UIView commitAnimations];   // commits the animation block.  This Block is done.

    
    [splashImageView removeFromSuperview];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
